package cn.deathgun.dcswebspringboot.service.impl

import cn.deathgun.dcswebspringboot.entity.PlayerEntity
import cn.deathgun.dcswebspringboot.mapper.PlayerMapper
import cn.deathgun.dcswebspringboot.mapper.UnittypeMapper
import cn.deathgun.dcswebspringboot.service.IPlayerService
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service



@Service
class PlayerServiceImpl : IPlayerService {
    @Autowired private lateinit var playerMapper: PlayerMapper
    @Autowired private lateinit var unittypeMapper: UnittypeMapper

    class PlayerStat(var splashType:String, var splashStat:Int, var ptsAdd:Int)
    override fun playerUpdate(ucid: String, action: Int):String {
        // actionId:
        // @ 1: teamkill
        // @ 2: landing
        // @ 3: dead
        val player = playerMapper.listPlayerByUcid(ucid)
        val playerStat = PlayerStat("", 0, 0)

        when (action) {
            1 -> { // TK
                playerStat.splashType = PlayerEntity::teamkill.name
                playerStat.splashStat = player.teamkill + 1
                playerStat.ptsAdd=-200
            }
            2 -> { // landing
                playerStat.splashType = PlayerEntity::landing.name
                playerStat.splashStat = player.landing + 1
                playerStat.ptsAdd=10
            }
            3 -> { // dead
                playerStat.splashType = PlayerEntity::dead.name
                playerStat.splashStat = player.dead + 1
                playerStat.ptsAdd=-100
            }
        }

        val updateWrapper = UpdateWrapper<PlayerEntity>()
            .set(playerStat.splashType, playerStat.splashStat)
            .set(PlayerEntity::pts.name, player.pts + playerStat.ptsAdd)
            .eq(PlayerEntity::ucid.name, ucid)
        playerMapper.update(null, updateWrapper)

        return if(playerStat.ptsAdd > 0){
            "你的分数变化: +${playerStat.ptsAdd}"
        } else {
            "你的分数变化: ${playerStat.ptsAdd}"
        }

    }


    override fun getPlayers(): List<PlayerEntity>
    = playerMapper.listAllPlayers();

    override fun getPlayerByName(name: String): PlayerEntity
    = playerMapper.listPlayerByName(name)

    override fun searchPlayer(name: String): List<PlayerEntity>
    = playerMapper.listPlayerByLikeName(name)

    override fun getPlayerInfo(ucid: String): PlayerEntity {
        return playerMapper.listPlayerByUcid(ucid)
    }

    override fun playerJoinGame(ucid: String, name: String): PlayerEntity { // 玩家进入
        if(playerMapper.listPlayerByUcid(ucid) === null) { // 首次进入的玩家
            playerMapper.insertNewPlayer(ucid, name)
        } else {
            val player = playerMapper.listPlayerByUcid(ucid)
            if(player.name != name) { // 检查玩家名称是否改变
                val updateWrapper = UpdateWrapper<PlayerEntity>()
                    .set(PlayerEntity::name.name, name)
                    .eq(PlayerEntity::ucid.name, player.ucid)
                playerMapper.update(null, updateWrapper)
            }
        }
        return playerMapper.listPlayerByUcid(ucid) // 返回新的玩家实体
    }

    override fun playerGetKill(typeName: String, ucid: String): String {

        val playerStat = PlayerStat("", 0, 0)
        val player = playerMapper.listPlayerByUcid(ucid)

        val unnittype = unittypeMapper.selectByTypeName(typeName)

        if(unnittype == null) {
            return "$typeName 没有在数据库中，联系管理员！"
        } else {
            when (unnittype.typeId) {
                1 -> { // 空中目标
                    playerStat.splashType = PlayerEntity::splashAA.name
                    playerStat.splashStat = player.splashAA+1
                    playerStat.ptsAdd = 50
                }
                2 -> { // 地面目标
                    playerStat.splashType = PlayerEntity::splashAG.name
                    playerStat.splashStat = player.splashAG+1
                    playerStat.ptsAdd = 10
                }
                3 -> { // 海上目标
                    playerStat.splashType = PlayerEntity::splashSHIP.name
                    playerStat.splashStat = player.splashSHIP+1
                    playerStat.ptsAdd = 300
                }
                4 -> { // 反辐射
                    playerStat.splashType = PlayerEntity::splashSEAD.name
                    playerStat.splashStat = player.splashSEAD+1
                    playerStat.ptsAdd = 20
                }
            }
            val updateWrapper = UpdateWrapper<PlayerEntity>()
                .set(playerStat.splashType, playerStat.splashStat)
                .set(PlayerEntity::pts.name, player.pts+playerStat.ptsAdd)
                .eq(PlayerEntity::ucid.name, ucid)
            playerMapper.update(null, updateWrapper)
            return if(playerMapper.update(null, updateWrapper) == 1) {
                "你获得了${playerStat.ptsAdd}积分"
            } else {
                "系统错误，联系管理员。"
            }
        }


    }


}